<?php

namespace app\admin\model\xilujob;

use think\Model;


class BlackWords extends Model
{

    

    

    // 表名
    protected $name = 'xilujob_black_words';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public static function init() {
        $regexUpdate = function ($row) {
            if(!isset($row['regex']) || !$row['regex']) {
                $charArr = preg_split('/(?<!^)(?!$)/u', $row->keyword);
                $searchArr = [];
                $lastIndex = count($charArr)-1;
                foreach($charArr as $i=>$char) {
                    if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $char)) {
                        //中文字符去掉右侧任意非中文字符
                        $searchArr[] = $i===$lastIndex ? $char : $char.'[^\x{4e00}-\x{9fa5}]*';
                    }else {
                        //英文字符两边去掉任意非字母
                        $searchArr[] = $i===$lastIndex ? $char : $char.'[^A-Za-z]*';
                    }
                }
                $row->regex = '/'.implode('', $searchArr).'/ui';
            }
        };
        self::beforeInsert($regexUpdate);
        self::beforeUpdate($regexUpdate);
    }







}
